<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Session;
use app\admin\model\Admin;

class Login extends Controller
{
    /*登陆页面
     * */
    public function index()
    {
        if (Session::has('username', 'admin')) {
            $this->redirect('admin/Index/index');
        }
        return $this->fetch('login');
    }

    /*数据处理
     * */
    public function doLogin(Request $request)
    {
        if ($request->isAjax()) {

            $data = $request->param()['data'];
//            halt($data);
            $check = $this->validate($data, 'admin.login');
//            halt($check);
            if (TRUE !== $check) {
                return return_json(1, $check);
            }
            $rel = Admin::mDoLogin($data);
            switch ($rel) {
                case 1:
                    return return_json(1, '请求参数失败！');
                    break;
                case 2:
                    return return_json(1, '账号或者密码错误！');
                    break;
                case 3:
                    return return_json(1, '账号或者密码错误！');
                    break;
                default:
                    return return_json(0, '登陆成功', 4, url('Index/index'));
            }
        }
        return return_json(1, '请求参数错误！');
    }

    /*退出
     * */
    public function logout()
    {
        Session::clear('admin');
        $this->redirect('index');
    }
}
